#!/bin/sh
# /**
# * Copyright (c) 2018, God's harbor
# * All rights reserved.
# * @file    mkcmd.sh
# * @brief   生成命令的脚本
# * @author  Jason Zhou,yzfcer@163.com
# * @version 1.0
# * @date    2021-09-15
# */

function usage()
{
    echo "mkcmd.sh usage:"
    echo "mkcmd.sh <op> <cmd_name>"
    echo "op :-a | -d"
    echo "-a:add cmd file"
    echo "-d:delete cmd file"
}


function check_args()
{
    if [ $# -lt 2 ]
    then
        usage
        exit
    fi

    if [ "$2" == "-h" ]
    then
        usage
        exit
    fi
}

function mkfile()
{
    CMD_NAME=$2
    FILENAME=cmd_${CMD_NAME}.cpp
    if [ -f $FILENAME ]
    then
        rm -f $FILENAME
    fi
    touch $FILENAME
}

function fill_file()
{
    CMD_NAME=$2
    FILENAME=cmd_${CMD_NAME}.cpp
    echo -e "#include <stdio.h>" >> $FILENAME
    echo -e "#include <string.h>" >> $FILENAME
    echo -e "#include <unistd.h>" >>  $FILENAME
    echo -e "#include \"streamaxtype.h\"" >>  $FILENAME
    echo -e "#include \"framework.h\"" >>  $FILENAME
    echo -e "#include \"streamaxdev.h\"" >>  $FILENAME
    echo -e "#if ENABLE_CMD_${CMD_NAME^^}" >> $FILENAME
    echo -e "using namespace std;" >> $FILENAME

    echo -e "static xint32_t cmd_${CMD_NAME}_demo(xint32_t argc,xbyte_t **argv)" >> $FILENAME
    echo -e "{" >> $FILENAME
    echo -e "    ///< TODO: implement command function here" >> $FILENAME
    echo -e "    return 0; " >> $FILENAME
    echo -e "};\n" >> $FILENAME

    echo -e "///<==================================================================================================" >> $FILENAME

    echo -e "static cmd_map_s cmd_map[] =" >> $FILENAME
    echo -e "{" >> $FILENAME
    echo -e "    ///< TODO: {3000, \"demo\",cmd_${CMD_NAME}_demo}," >> $FILENAME
    echo -e "};\n" >> $FILENAME

    echo -e "XCMD_INFO($CMD_NAME)" >> $FILENAME
    echo -e "{" >> $FILENAME
    echo -e "    ///< TODO: printf(\"show cmd info\\\n\");" >> $FILENAME
    echo -e "}\n" >> $FILENAME


    echo -e "XCMD_USAGE($CMD_NAME)" >> $FILENAME
    echo -e "{" >> $FILENAME
    echo -e "    printf(\"$CMD_NAME usage:\\\n\");" >> $FILENAME
    echo -e "    ///<TODO: printf(\"show $CMD_NAME usage:\\\n\");" >> $FILENAME
    echo -e "}\n" >> $FILENAME

    echo -e "XCMD_FUNC($CMD_NAME,argc,argv)" >> $FILENAME
    echo -e "{" >> $FILENAME
    echo -e "    XCMD_PARASE($CMD_NAME);" >> $FILENAME
    echo -e "}\n" >> $FILENAME
    echo -e "XCMD_DEF($CMD_NAME);" >> $FILENAME
    echo -e "#endif // #if ENABLE_CMD_${CMD_NAME^^}\n" >> $FILENAME
}

function add_header_macro()
{
    CMD_NAME=$2
    line=`cat -n ./framework.h | grep ENABLE_CMD_${CMD_NAME^^} | awk '{print $1}'`
    if [ x"$line" != x ]
    then
        echo "framework.h:ENABLE_CMD_${CMD_NAME^^} has been exist"
        exit
    fi

    line=`cat -n ./framework.h | grep ADD_CMD_MACRO_ABOVE | awk '{print $1}'`
    if [ $line -eq 0 ]
    then
        echo "framework.h:no keywords ADD_CMD_MACRO_ABOVE"
        exit
    fi
    UPCMD=${CMD_NAME^^}
    echo "add ENABLE_CMD_${CMD_NAME^^},line="$line
    sed -i "$[ line ]i #define ENABLE_CMD_${UPCMD} 1" framework.h
}

function add_source_macro()
{
    CMD_NAME=$2
    line=`cat -n ./framework.cpp | grep ENABLE_CMD_${CMD_NAME^^} | awk '{print $1}'`
    if [ x"$line" != x ]
    then
        echo "framework.cpp:ENABLE_CMD_${CMD_NAME^^} has been exist"
        exit
    fi

    line=`cat -n ./framework.cpp | grep ADD_CMD_DECLARE_ABOVE | awk '{print $1}'`
    if [ $line -eq 0 ]
    then
        echo "framework.cpp:no keywords ADD_CMD_DECLARE_ABOVE"
        exit
    fi

    echo "add ENABLE_CMD_${CMD_NAME^^},line="$line
    UPCMD=${CMD_NAME^^}
    sed -i "$[ line ]i #if ENABLE_CMD_${UPCMD}" framework.cpp
    sed -i "$[ line + 1 ]i XCMD_DECLARE($CMD_NAME);" framework.cpp
    sed -i "$[ line + 2 ]i #endif" framework.cpp


    line=`cat -n ./framework.cpp | grep ADD_CMD_REGISTER_ABOVE | awk '{print $1}'`
    if [ $line -eq 0 ]
    then
        echo "framework.cpp:no keywords ADD_CMD_REGISTER_ABOVE"
        exit
    fi
    echo "add ENABLE_CMD_${UPCMD},line="$line
    UPCMD=${CMD_NAME^^}
    sed -i "$[ line ]i #if ENABLE_CMD_${UPCMD}" framework.cpp
    sed -i "$[ line + 1 ]i \ \ \ \ CmdRegister(XCMD($CMD_NAME),0);" framework.cpp
    sed -i "$[ line + 2 ]i #endif" framework.cpp
}

function add_cmd()
{
    mkfile $@
    fill_file $@
    add_header_macro $@
    add_source_macro $@
}

function del_cmd()
{
    CMD_NAME=$2
    FILENAME=cmd_${CMD_NAME}.cpp
    if [ -f $FILENAME ]
    then
        echo "delete file:"$FILENAME
        rm -f $FILENAME
    fi
    line=`cat -n ./framework.h | grep ENABLE_CMD_${CMD_NAME^^} | awk '{print $1}'`
    if [ x"$line" != x ]
    then
        echo "framework.h:delete ENABLE_CMD_${CMD_NAME^^},line="$line
        sed -i "$[ line ]d" framework.h
    fi

    lines=`cat -n ./framework.cpp | grep ENABLE_CMD_${CMD_NAME^^} | awk '{print $1}'`
    offset=0
    for line in $lines
    do
        li=$[ line - $offset ]
        lineend=$[ li + 2 ]
        echo "framework.cpp:delete ENABLE_CMD_${CMD_NAME^^},line="$li
        sed -i "${li},${lineend}d" framework.cpp
        offset=$[ offset + 3 ]
    done
}

function main()
{
    check_args $@
    if [ "$1" == "-a" ]
    then
        add_cmd $@
    elif [ "$1" == "-d" ]
    then
        del_cmd $@
    else
        usage
    fi
}
main $@
